package com.drcoffee.drcoffeemall.service.impl;

import com.drcoffee.drcoffeemall.entry.CssOrientation;
import com.drcoffee.drcoffeemall.entry.Explosion;
import com.drcoffee.drcoffeemall.entry.Result;
import com.drcoffee.drcoffeemall.entry.Type;
import com.drcoffee.drcoffeemall.mapper.ExplosionMapper;
import com.drcoffee.drcoffeemall.mapper.FilePathMapper;
import com.drcoffee.drcoffeemall.mapper.MachineMapper;
import com.drcoffee.drcoffeemall.mapper.TypeMapper;
import com.drcoffee.drcoffeemall.service.ExplosionService;
import com.drcoffee.drcoffeemall.util.FileUploadDownloadUtils;
import com.drcoffee.drcoffeemall.util.OSSUtil;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ExplosionServiceImpl implements ExplosionService {

    final
    ExplosionMapper explosionMapper;

    final
    TypeMapper typeMapper;

    final
    FilePathMapper filePathMapper;

    final
    MachineMapper machineMapper;

    final
    OSSUtil ossUtil;


    public ExplosionServiceImpl(ExplosionMapper explosionMapper, OSSUtil ossUtil, TypeMapper typeMapper, FilePathMapper filePathMapper, MachineMapper machineMapper) {
        this.explosionMapper = explosionMapper;
        this.ossUtil = ossUtil;
        this.typeMapper = typeMapper;
        this.filePathMapper = filePathMapper;
        this.machineMapper = machineMapper;
    }

    /*
     * 根据ID查询爆炸图
     * */
    @Override
    public Map<String, Object> getExplosionById(Integer id) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            Explosion explosion = explosionMapper.getExplosionById(id);
            resultMap.put("message", "查询成功");
            resultMap.put("explosion", explosion);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("message", "查询失败");
            resultMap.put("reason", e.toString());
        }
        return resultMap;
    }

    /*
     * 根据机器ID查询关联爆炸图
     * */
    @Override
    public ResponseEntity<Object> getExplosionByMachineId(Integer machine_id) {
        return new ResponseEntity<>(explosionMapper.getExplosionByMachineId(machine_id), HttpStatus.OK);
    }

    /*
     * 查询包插图关联的定位点
     * */
    @Override
    public Map<String, Object> getCssOrientation(Integer id) {
        Map<String, Object> resultMap = new HashMap<>();
        List<CssOrientation> cssOrientation = explosionMapper.getCssOrientation(id);
        resultMap.put("result", 1);
        resultMap.put("cssOrientationList", cssOrientation);
        System.out.println(cssOrientation);
        return resultMap;
    }

//    @Override
//    public Map<String, Object> fileUpload(MultipartFile file, String name, Integer machineId) throws IOException {
//        String imgFileUrl = explosionMapper.getImgFileUrl(machineId);
//        return FileUploadDownloadUtils.uploadFile(file, imgFileUrl + "\\explosion", name);
//    }

    @Override
    public Map<String, Object> addExplosion(Explosion explosion) {
        Map<String, Object> resultMap = new HashMap<>();
        //explosion.setImage_url(data_img_init + "/image" + explosion.getImage_url().replaceAll("\\\\", "/"));
        boolean result = explosionMapper.addExplosion(explosion);
        resultMap.put("result", result);
        if (result) {
            resultMap.put("message", "添加成功");
        } else {
            resultMap.put("message", "添加失败");
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> deleteExplosion(Explosion explosion) {
        Map<String, Object> resultMap = new HashMap<>();
        List<CssOrientation> cssOrientation = explosionMapper.getCssOrientation(explosion.getId());
        if (cssOrientation.isEmpty()) {
            boolean deleteResult = FileUploadDownloadUtils.deleteFile(explosion.getImage_url());
            if (deleteResult) {
                boolean result = explosionMapper.deleteExplosion(explosion.getId());
                resultMap.put("result", result);
                if (result) {
                    resultMap.put("message", "删除成功");
                } else {
                    resultMap.put("message", "删除失败");
                }
            }
        } else {
            resultMap.put("result", false);
            resultMap.put("message", "删除失败,该爆炸图存在关联定位点");
        }
        return resultMap;
    }

    //添加爆炸图定位点
    @Override
    public Map<String, Object> addCssOrientation(Integer explosion_id) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            boolean result = explosionMapper.addCssOrientation(explosion_id);
            resultMap.put("result", result);
            resultMap.put("message", "添加成功");
        } catch (Exception e) {
            resultMap.put("message", "添加失败");
            resultMap.put("reason", e.toString());
        }
        return resultMap;
    }

    /*
     * 修改爆炸图圆点定位
     * */
    @Override
    public Map<String, Object> setCssOrientation(CssOrientation cssOrientation) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            boolean result = explosionMapper.setCssOrientation(cssOrientation);
            resultMap.put("result", result);
            resultMap.put("message", "保存成功");
        } catch (Exception e) {
            resultMap.put("message", "保存失败");
            resultMap.put("reason", e.toString());
        }
        return resultMap;
    }

    @Override
    public ResponseEntity<Object> dotSetExplosion(Integer id, Integer decompose_id) {
        explosionMapper.dotSetExplosion(id, decompose_id);
        // 判断定位点是否绑定分解
        Integer bindPart = explosionMapper.getBindPart(id);
        if (bindPart == 0) {
            explosionMapper.setIsFj(id, "Y");
        } else {
            explosionMapper.setIsFj(id, "S");
        }
        return new ResponseEntity<>(true, HttpStatus.OK);
    }

    /*
     * 删除定位点
     * */
    @Override
    public Map<String, Object> deleteCssOrientation(Integer id) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            boolean result = explosionMapper.deleteCssOrientation(id);
            resultMap.put("result", result);
            resultMap.put("message", "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("message", "删除失败");
            resultMap.put("reason", e.toString());
        }
        return resultMap;
    }

    /*
     * 定位点取消分解
     * */
    @Override
    public Map<String, Object> setDismantleN(Integer id) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            boolean result = explosionMapper.setDismantleN(id);
            resultMap.put("result", result);
            resultMap.put("message", "取消成功");
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("message", "取消失败");
            resultMap.put("reason", e.toString());
        }
        return resultMap;
    }

    @Override
    public ResponseEntity<Object> bindPart(Integer dotId, List<String> attachments) {
        explosionMapper.delBindPart(dotId);
        explosionMapper.bindingPart(dotId, attachments);
        // 判断定位点是否绑定分解
        Integer decompose = explosionMapper.getCssOrientationDecomposeById(dotId);
        if (decompose != null) {
            explosionMapper.setIsFj(dotId, "S");
        }
        return new ResponseEntity<>(true, HttpStatus.OK);
    }

    @Override
    public ResponseEntity<Object> insertData(MultipartFile file, String fileName, Map<String, Object> explosion) {
        Integer type_id = machineMapper.getMachineTypeId((Integer) explosion.get("machine_id"));
        Type type = typeMapper.getTypeById(type_id);
        String uploadPath = filePathMapper.getUploadPath(type.getPath_bsh(), 2);
        Result upload = ossUtil.upload(file, uploadPath, fileName);
        if (upload.getResult() != 1) {
            return new ResponseEntity<>(false, HttpStatus.OK);
        }
        explosion.put("image_url", upload.getObject());
        explosionMapper.insertData(explosion);
        return new ResponseEntity<>(true, HttpStatus.OK);
    }
}
